package com.valuegroup.smarttodo.common;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public abstract class CommonRecyclerAdapter<T> extends RecyclerView.Adapter<BaseViewHolder> {
    private Context mContext;
    private LayoutInflater mInflater;
    // 数据
    private List<T> mData = new ArrayList<>();
    // 布局
    private int mainLayoutId = 0;
    private int headLayoutId = 0;
    private int bottomLayoutId = 0;
    private int[] appendLayoutList;

    public void setMainLayoutId(int mainLayoutId) {
        this.mainLayoutId = mainLayoutId;
    }

    public void setHeadLayoutId(int headLayoutId) {
        this.headLayoutId = headLayoutId;
    }

    public void setBottomLayoutId(int bottomLayoutId) {
        this.bottomLayoutId = bottomLayoutId;
    }

    public void setAppendLayoutList(int... appendLayout) {
        this.appendLayoutList = appendLayout;
    }

    public CommonRecyclerAdapter(Context context, List<T> data) {
        this.mContext = context;
        this.mInflater = LayoutInflater.from(context);
        this.mData.addAll(data);
    }


    @NonNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = mInflater.inflate(viewType, parent, false);
        return new BaseViewHolder(v, viewType);
    }

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder holder, int position) {
        // 设置点击和长按事件
        if (mItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mItemClickListener.onItemClick(position);
                }
            });
        }
        if (mLongClickListener != null) {
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    return mLongClickListener.onLongClick(position);
                }
            });
        }

        int viewType = getItemViewType(position);
        if (viewType == mainLayoutId) {
            bindMainView(holder, position);
        } else if (hasHead() && viewType == headLayoutId) {
            bindHeadView(holder, position);
        } else if (hasBottom() && viewType == bottomLayoutId) {
            bindBottomView(holder, position);
        } else if (hasAppend()) {
            bindAppendView(holder, position);
        }
    }

    public T getData(int position) {
        return mData.get(hasHead() ? position - 1 : position);
    }

    /**
     * 利用抽象方法回传出去，每个不一样的Adapter去设置
     */
    public abstract void bindHeadView(BaseViewHolder v, int position);

    public abstract void bindBottomView(BaseViewHolder v, int position);

    public abstract void bindMainView(BaseViewHolder v, int position);

    public abstract void bindAppendView(BaseViewHolder v, int position);

    @Override
    public int getItemCount() {
        return (hasHead() ? 1 : 0) + getDataSize() + (hasAppend() ? appendLayoutList.length : 0) + (hasBottom() ? 1 : 0);
    }

    public int getDataSize() {
        return mData.size();
    }


    public boolean hasHead() {
        return headLayoutId != 0;
    }

    public boolean hasBottom() {
        return bottomLayoutId != 0;
    }

    public boolean hasAppend() {
        return appendLayoutList != null && appendLayoutList.length > 0;
    }

    @Override
    public int getItemViewType(int position) {

        // 头布局
        if (hasHead() && position == 0) {
            return headLayoutId;
        }

        // 追加布局
        if (hasAppend()) {
            for (int i = 0; i < appendLayoutList.length; i++) {
                if (position == ((hasHead() ? 1 : 0) + getDataSize() + i)) {
                    return appendLayoutList[i];
                }
            }
        }

        if (hasBottom()) {
            if (position == (hasHead() ? 1 : 0) + getDataSize() + (hasAppend() ? appendLayoutList.length : 0)) {
                return bottomLayoutId;
            }
        }

        return mainLayoutId;
    }

    public void addItem(int position, T item) {
        mData.add(mData.size(), item);
        notifyItemInserted(mData.size() + (hasHead() ? 1 : 0));
    }

    // 刷新列表数据
    public void refreshListData(List<T> datas) {
        int previousSize = mData.size();
        mData.clear();
        notifyItemRangeRemoved(0, (hasHead() ? 1 : 0) + previousSize + (hasAppend() ? appendLayoutList.length : 0) + (hasBottom() ? 1 : 0));
        mData.addAll(datas);
        notifyItemRangeInserted(0, (hasHead() ? 1 : 0) + getDataSize() + (hasAppend() ? appendLayoutList.length : 0) + (hasBottom() ? 1 : 0));
    }

    public void refreshData(int position, T data) {
        boolean hasHead = headLayoutId != 0;

        mData.set(position - (hasHead ? 1 : 0), data);
        notifyItemChanged(position, data);
    }

    // 设置条目点击和长按事件
    private OnItemClickListener mItemClickListener;
    private OnLongClickListener mLongClickListener;

    public void setOnItemClickListener(OnItemClickListener itemClickListener) {
        this.mItemClickListener = itemClickListener;
    }

    public void setOnLongClickListener(OnLongClickListener longClickListener) {
        this.mLongClickListener = longClickListener;
    }


    public interface OnItemClickListener {
        void onItemClick(int positon);
    }

    public interface OnLongClickListener {
        boolean onLongClick(int position);
    }

    public Context getmContext() {
        return mContext;
    }
}
